const {PostTypeModel} = require('../../model/PostType/PostType');
const tools = require('../../tools/public');
// 添加一级职位类型信息
exports.Add_level1 = function (arg) {
    return new Promise((resolve, reject) => {
        const postTypeModel = new PostTypeModel({
            PostTypeName: arg.PostTypeName,
            PostTypeDescribe: arg.PostTypeDescribe,
            PostTypeCreateTime: new Date().getTime(),
            ChildrenList: []
        })
        postTypeModel.save((error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
};
// 查询count
exports.Sel_Count = function (arg) {
    return new Promise((resolve, reject) => {
        PostTypeModel.count({...arg}, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}
// 添加二级职位类型信息
exports.Add_level2 = function (parentId, arg) {
    return new Promise((resolve, reject) => {
        PostTypeModel.find({'_id': parentId}, function (error, data) {
            if (error) return reject(error)
            if (data.length === 0) return resolve({code: 204, msg: "该parentId不存在"});

            let ChildrenList = data[0].ChildrenList;
            let {PostTypeName} = arg
            let arr = [false]; // 判断名字是否重复
            for (let i in ChildrenList) {
                if (ChildrenList[i].PostTypeName === PostTypeName) {
                    arr[0] = true
                    break;
                }
            }
            if (arr[0]) return resolve({code: 204, msg: "该职位名字已经存在！"})

            // 添加文档中的数组数据
            PostTypeModel.updateOne({
                _id: parentId
            }, {
                $addToSet: {
                    ChildrenList: [{
                        PostTypeName: arg.PostTypeName,
                        PostTypeDescribe: arg.PostTypeDescribe,
                        PostTypeCreateTime: new Date().getTime(),
                        _id: tools.create_timeName()
                    }]
                }
            }, function (error2, data2) {
                if (error2) return reject(error2)
                resolve({code: 200, msg: "添加成功！", data: data2})
            })
        })
    })
};


/* 查询职位信息(模糊/分页/多条件)
* */
exports.Sel = function (PageNumber, PageSize, arg) {
    return new Promise((resolve, reject) => {
        PostTypeModel.find({...arg}).skip(PageNumber * PageSize).limit(PageSize).exec((error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
};

// 查询文档中的对象/数组的属性
/*
*   @key           需要匹配的键名
*   @value         需要匹配的值
*   @filterOptions 需要过滤的属性
* */
// exports.Sel_quoteType = function (key, value, filterOptions) {
//     return new Promise((resolve, reject) => {
//         PostTypeModel.find({[key]: value}, filterOptions, (error, result) => {
//             if (error) return reject(error)
//             resolve(result)
//         })
//     })
// }


// 修改职位信息   层级1
exports.Upd_ById_forLevel1 = function (_id, UpdateObject) {
    return new Promise((resolve, reject) => {
        PostTypeModel.findByIdAndUpdate({_id}, UpdateObject, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}
// 修改职位信息   层级2
exports.Upd_ById_forLevel2 = function (_id,parentId,UpdateObject) {
    return new Promise((resolve, reject) => {
        PostTypeModel.update({_id: parentId,"ChildrenList._id":_id}, {
            '$set': UpdateObject
        }, function (error, data) {
            if (error) return reject(error)
            resolve(data)
        })
    })
}


// 删除职位信息(删除单条) 层级1
exports.Del_One_ById_forLevel1 = function (value) {
    return new Promise((resolve, reject) => {
        PostTypeModel.deleteOne({'_id': value}, (error, result) => {
            if (error) return reject(error)
            resolve(result)
        })
    })
}
// 删除职位信息(删除单条) 层级2
exports.Del_One_ById_forLevel2 = function (_id, parentId) {
    return new Promise((resolve, reject) => {
        PostTypeModel.update({_id: parentId}, {
            '$pull': {ChildrenList: {_id}}
        }, function (error, data) {
            if (error) return reject(error)
            resolve(data)
        })
    })
}

// 删除职位信息(删除多条)
// exports.Del_Many_ById = function (valueList) {
//     return new Promise((resolve, reject) => {
//         PostTypeModel.deleteMany({'_id': {$in: valueList}}, (error, result) => {
//             if (error) return reject(error)
//             resolve(result)
//         })
//     })
// }